package com.app.dao.impl;

import java.util.Collection;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

import com.app.dao.SeriesDao;
import com.app.entity.Series;

@Repository
public class SeriesDaoImpl extends HibernateDaoSupport implements SeriesDao {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	public void SaveOrUpdateSeries(Series series) {
		logger.debug("增加/修改品牌/系列");
		Assert.notNull(series);
		getHibernateTemplate().saveOrUpdate(series);
	}

	public void deleteSeries(String idStr) {
		logger.debug("删除品牌/系列");
		if(StringUtils.isBlank(idStr)) return ;
		String[] arrId = idStr.split(",");
		for(String id : arrId){
			if(!StringUtils.isBlank(id)){
				getHibernateTemplate().delete(getHibernateTemplate().load(Series.class, Integer.valueOf(id)));				
			}
		}
	}

	@SuppressWarnings("unchecked")
	public Collection<Series> findSeriesByParameter(Series series) {
		logger.debug("查询品牌/系列");
		if (series.getFlag() > 0)
			return getHibernateTemplate().find("from Series where flag = " + series.getFlag());
		else
			return getHibernateTemplate().find("from Series");
	}

	public Series load(Integer id) {
		logger.debug("查询单个品牌/系列");
		return (Series) getHibernateTemplate().load(Series.class, id);
	}

}
